<?php

class Admin_Model_Feedback {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Admin_Model_DbTable_Feedback();
    }

    /**
     * 
     * @param type $data
     */
    public function insert($data) {
        $this->_dbtable->insert($data);
    }

    /**
     * 
     * @return array
     */
    public function fetchAll($todayOnly = false) {
        $select = $this->_dbtable->select();
        $result = $this->_dbtable->fetchAll($select);
        $resultArray = array();
        foreach ($result as $data) {
            $response = $this->convertDataToArray($data);
            if ($todayOnly) {
                if (strtotime(date('d-m-Y', time())) == strtotime($response['date_string'])) {
                    array_push($resultArray, $response);
                }
            } else {
                array_push($resultArray, $response);
            }
        }
        return $resultArray;
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function convertDataToArray($data) {
        $resultArray = array();
        $detailsJson = $data['details'];
        $djson = json_decode($detailsJson);
        $resultArray['id'] = $data['id'];
        $resultArray['name'] = $djson->name;
        $resultArray['mobile'] = $djson->mobile;
        $resultArray['email'] = $djson->email;
        $resultArray['subject'] = $djson->subject;
        $resultArray['message'] = $djson->message;
        $resultArray['date_string'] = date('m-d-Y', $djson->date);
        $resultArray['timestamp'] = $djson->date;
        return $resultArray;
    }

}

